package com.ttms.service.impl;

import com.ttms.dao.*;
import com.ttms.dao.impl.*;
import com.ttms.pojo.Info;
import com.ttms.pojo.Play;
import com.ttms.pojo.Schedule;
import com.ttms.pojo.Ticket;
import com.ttms.service.TicketService;

import java.math.BigDecimal;
import java.util.*;

/**
 *      * play_id的值获取Ticket表中的数据个数count
 *      * 票价*count = 销售额
 *      * 根据演出计划查询影片名字和票价
 *      * 在订单信息里根据演出计划查询数据个数，即该演出计划的售出个数  票价*count = 销售额
 *      * 在ticket表中获取影厅ID
 *      * 在排片表中获取影厅ID
 *      *
 *      * 获取影厅的行号和列号 销售额 = count / (行号*列号)
 */
public class TicketServiceImpl implements TicketService {
    private PlayDao playDao = new PlayDaoImpl();
    private TicketDao ticketDao = new TicketDaoImpl();
    private ScheduleDao scheduleDao = new ScheduleDaoImpl();
    private SaleDao saleDao = new SaleDaoImpl();
    private SaleItemDao saleItemDao = new SaleItemDaoImpl();
    private StudioDao studioDao = new StudioDaoImpl();

//    @Override
//    public List<Ticket> queryTicketByUserId(int user_id) {
//        return ;
//    }

    @Override
    public List<Info> queryInfos() {
        List<Play> plays = playDao.queryPlays();
        List<Schedule> schedules = scheduleDao.querySchedules();
        int count = scheduleDao.queryForPageTotalCount();
        ArrayList<Info> infos = new ArrayList<Info>();
        for (Schedule schedule:schedules){
            Info info = new Info();
            info.setSched_id(schedule.getSched_id());
            info.setPlay_id(schedule.getPlay_id());
            Integer studio_col_count = studioDao.queryStudioById(schedule.getStudio_id()).getStudio_col_count();
            Integer studio_row_count = studioDao.queryStudioById(schedule.getStudio_id()).getStudio_row_count();
            info.setAttendance(new BigDecimal(studio_col_count * studio_row_count));
            infos.add(info);
        }
        for (Info info: infos) {
            for (Play play:plays) {
                if (info.getPlay_id()==play.getPlay_id()){
                    info.setPlay_name(play.getPlay_name());
                    info.setPlay_ticket_price(play.getPlay_ticket_price());
                    Integer num = ticketDao.queryForTotalCountBySchedule(info.getSched_id());
                    if (num==null){
                        BigDecimal bignum= new BigDecimal(0);
                        BigDecimal price = info.getPlay_ticket_price();
                        info.setTotal_price(price.multiply(bignum));
                        BigDecimal studio_seat = info.getAttendance();
                        info.setAttendance(bignum.divide(studio_seat,4,BigDecimal.ROUND_HALF_DOWN));
                    }else{
                        BigDecimal bignum = new BigDecimal(num);
                        BigDecimal price = info.getPlay_ticket_price();
                        info.setTotal_price(price.multiply(bignum));
                        BigDecimal studio_seat = info.getAttendance();
                        info.setAttendance(bignum.divide(studio_seat,4,BigDecimal.ROUND_HALF_DOWN));
                    }
                }
            }
        }
        return infos;
    }

    @Override
    public Ticket findTicketById(int ticket_id) {
        return ticketDao.findTicketById(ticket_id);
    }

    @Override
    public int deleteTicket(int ticket_id) {
        return ticketDao.deleteTicketById(ticket_id);
    }

    @Override
    public Ticket findTicketBySeatId(int sched_id,  int seat_id) {
        return ticketDao.findTicketIdBySeatId(sched_id ,seat_id);
    }

    @Override
    public int createTicket(Ticket ticket) {
        return ticketDao.addTicket(ticket);
    }
}
